$(function() {
	// 初始化表格
	$("#dataTable").jqxDataTable({
		width : "100%",
		source : getSource(),
		pageable : true,
		serverProcessing : true,
		pageSize : 15,
		pagerButtonsCount : 10,
		columnsResize : true,
		sortable : true,
		selectionMode : "singleRow",
		columns : [ {
			text : "序号",
			width : "4%",
			dataField : "id",
			cellsRenderer : function(row) {
				return (row + 1).toString();
			}
		}, {
			text : "订单号",
			width : "8%",
			dataField : "orderid"
		}, {
			text : "商店",
			width : "10%",
			dataField : "shop_name"
		}, {
			text : "用户名",
			width : "10%",
			dataField : "user_name"
		}, {
			text : "收件人",
			width : "6%",
			dataField : "consignee_name"
		}, {
			text : "快递",
			width : "6%",
			dataField : "express_name",
		},{
			text : "快递单号",
			width : "6%",
			dataField : "express_number",
		},{
			text : "快递费用",
			width : "6%",
			dataField : "express_price",
		}, {
			text : "总金额",
			width : "3%",
			dataField : "aggregate_amount"
		}, {
			text : "实际支付金额",
			width : "3%",
			dataField : "actual_price"
		}, {
			text : "积分支付",
			width : "3%",
			dataField : "pointpay"
		}, {
			text : "钱包支付",
			width : "3%",
			dataField : "walletpay"
		}, {
			text : "状态",
			dataField : "status",
			width : "4%",
			cellsRenderer : function(row, column, value, rowData) {
				return getStatus(value, true);
			}
		}
		/*, {
			text : "支付商家状态",
			dataField : "pay_seller_status",
			width : "12%",
			cellsRenderer : function(row, column, value, rowData) {
				var msg = "";
				if (value === "" || value === undefined || value === null) {
					return '<input type="button" class="btn btn-sm btn-danger" disabled="disabled" value="异常">';
				}
				if (rowData.status === 1 && value === false) {
					return '<a  class="btn-sm btn-success" href="javascript:;" onclick="nowTransfer(\''+rowData.uuid+'\')">立刻付款</a>';
					//return '<a  class="btn-sm btn-success" href="/manager/order/transfer.do?uuid='+rowData.uuid+'" target="_blank">立刻付款</a>';
					//'<input type="button" class="btn btn-sm btn-success" disabled="disabled" onclick="nowTransfer(\''+rowData.uuid+'\')" value="立刻付款">';
				}
				return '<input type="button" class="btn btn-sm btn-warning"  disabled="disabled" value="已付款">';
			}
		}*/
		, {
			text : "类型",
			dataField : "order_type",
			width : "4%",
			cellsRenderer : function(row, column, value, rowData) {
				return getOrder_type(value);
			}
		}, {
			text : "支付时间",
			dataField : "paytime",
			width : "12%",
			cellsRenderer : function(row, column, value, rowData) {
				if (value === "" || value === undefined || value === null) {
					return "";
				}
				var date = value instanceof Date ? value : new Date(value);
				return date.Format("yyyy-MM-dd hh:mm:ss");
			}
		}, {
			text : "创建时间",
			dataField : "createtime",
			width : "12%",
			cellsRenderer : function(row, column, value, rowData) {
				if (value === "" || value === undefined || value === null) {
					return "";
				}
				var date = value instanceof Date ? value : new Date(value);
				return date.Format("yyyy-MM-dd hh:mm:ss");
			}
		} ],
	});

	/**
	 * 双击查看详细
	 */
	$("#dataTable").on("rowDoubleClick", function(event) {
		var args = event.args;
		var row = args.row;
		var index = args.index;
		viewOrderDetailed(row);
	});
	/**
	 * 查看
	 */
	$("#view").click(function() {
		var rows = isSelected();
		if (rows !== null) {
			viewOrderDetailed(rows[0]);
		}
	});

	/**
	 * 状态选择
	 */
	$("#status").find("li").on("click", function() {
		var status_title = $(this).find("a").text();
		var status_value = $(this).attr("value");
		$("#status_title").text(status_title);
		$("#status").attr("value", status_value + "");
	});

	/**
	 * 搜索
	 */
	$("#search").click(function() {
		var orderId = $("#orderId").val();
		var shop_name = $("#shop_name").val();
		var nickname = $("#nickname").val();
		var status = $("#status").attr("value");
		var json = {
			orderId : orderId,
			shop_name : shop_name,
			nickname : nickname,
			status : status != -1 ? status : ""
		}
		$("#dataTable").jqxDataTable({
			source : getSource(json)
		});
	});

});

//立刻付款
function nowTransfer(uuid){
	
	var count = CookieUtils.get(uuid);
	if(count==undefined||count==null||count==""||count==0){
		//保存半天
		CookieUtils.set(uuid,1,1/24);
		goTransfer(uuid);
	}else{
		layer.alert("系统记录你今天已经点过此订单按钮，今天已禁止操作。如果已经跳转成功，并输入支付密码支付成功，请勿重新操作！！！支付宝方面可能会有延迟，请耐心等待订单处理完成。如并未支付，请清理缓存或1小时候后重新操作。");
	}
	
}

function goTransfer(uuid){
	var layerindex = layer.confirm('确认支付吗？', {
		  btn: ['确认','取消'] //按钮
	}, function(){
		layer.close(layerindex);
		window.open("/manager/order/transfer.do?uuid="+uuid);
	}, function(){
	  
	});
}

/**
 * 查看商品详细
 * 
 * @param row
 */
function viewOrderDetailed(row) {
	_editerIndex = layer.open({
		id : "expressEditer",
		title : "订单详细",
		type : 1,
		zIndex : 99999999,
		shadeClose : false,
		skin : 'layui-layer-rim',
		area : [ '1024px', '600px' ],
		content : $("#content").html(),
		success : function(e) {
			eConetnt = $(e);
		}
	});
	// $("#uuid").val(row.uuid);
	eConetnt.find("#orderid").val(row.orderid);
	eConetnt.find("#user_name").val(row.user_name);
	eConetnt.find("#consignee_name").val(row.consignee_name);
	eConetnt.find("#express_name").val(row.express_name);
	eConetnt.find("#aggregate_amount").val(row.aggregate_amount);
	eConetnt.find("#actual_price").val(row.actual_price);
	eConetnt.find("#status").val(getStatus(row.status));
	eConetnt.find("#confirmtime").val(getDate(row.confirmtime));
	eConetnt.find("#paytime").val(getDate(row.paytime));
	eConetnt.find("#createtime").val(getDate(row.createtime));
	eConetnt.find("#shop_name").val(row.shop_name);
	detailed_datatable(row.uuid);
}

/**
 * 初始化商品详细
 */
function detailed_datatable(uuid) {
	// 设置数据源
	var tableSource = new $.jqx.dataAdapter({
		dataType : "json",
		dataFields : [ {
			name : "id",
			type : "int"
		}, {
			name : "goods_name",
			type : "string"
		}, {
			name : "price",
			type : "double"
		}, {
			name : "quantity",
			type : "int"
		}, {
			name : "subtotal",
			type : "double"
		} ],
		id : "uuid",
		url : "/manager/order/getOrderDetailedByUuid.do?uuid=" + uuid
	});
	// 初始化表格
	eConetnt.find("#detailed-datatable").jqxDataTable({
		width : "95%",
		source : tableSource,
		pageable : true,
		serverProcessing : true,
		pageSize : 3,
		pagerButtonsCount : 10,
		columnsResize : true,
		sortable : true,
		columns : [ {
			text : "序号",
			width : 50,
			cellsRenderer : function(row) {
				return (row + 1).toString();
			}
		}, {
			text : "商品名称",
			dataField : "goods_name"
		}, {
			text : "价格",
			dataField : "price"
		}, {
			text : "数量",
			dataField : "quantity"
		}, {
			text : "小计",
			dataField : "subtotal",
		} ],
	}).css("min-height", "320px");
}

/**
 * 获取数据源
 */
function getSource(json) {
	var url = "/manager/order/getOrderListBySelective.do?test=1";
	if (json !== undefined) {
		if (json.orderId !== undefined && json.orderId !== null
				&& json.orderId !== "") {
			url += "&orderId=" + json.orderId;
		}
		if (json.shop_name !== undefined && json.shop_name !== null
				&& json.shop_name !== "") {
			url += "&shop_name=" + json.shop_name;
		}
		if (json.nickname !== undefined && json.nickname !== null
				&& json.nickname !== "") {
			url += "&nickname=" + json.nickname;
		}
		if (json.status !== undefined && json.status !== null
				&& json.status !== "") {
			url += "&status=" + json.status;
		}
	}
	// 设置数据源
	var tableSource = new $.jqx.dataAdapter({
		dataType : "json",
		dataFields : [ {
			name : "id",
			type : "int"
		}, {
			name : "uuid",
			type : "string"
		}, {
			name : "orderid",
			type : "string"
		}, {
			name : "shop_name",
			type : "string"
		}, {
			name : "user_name",
			type : "string"
		}, {
			name : "consignee_name",
			type : "string"
		}, {
			name : "express_name",
			type : "string"
		}, {
			name : "express_price",
			type : "double"
		},{
			name : "express_number",
			type : "string"
		}, {
			name : "aggregate_amount",
			type : "double"
		}, {
			name : "actual_price",
			type : "double"
		}, {
			name : "status",
			type : "int"
		}, {
			name : "pay_seller_status",
			type : "int"
		}, {
			name : "order_type",
			type : "int"
		}, {
			name : "pointpay",
			type : "double"
		}
		, {
			name : "walletpay",
			type : "double"
		}, {
			name : "confirmtime",
			type : "date"
		}, {
			name : "paytime",
			type : "date"
		}, {
			name : "createtime",
			type : "date"
		}, {
			name : "shop_name",
			type : "string"
		} ],
		id : "uuid",
		url : url
	}, {
		formatData : function(data) {
			data.pagenum = data.pagenum + 1;
			return data;
		}
	});
	return tableSource;
}

/**
 * 是否选中一项
 * 
 * @returns
 */
function isSelected() {
	var rows = $("#dataTable").jqxDataTable("getSelection");
	if (rows.length < 1) {
		layer.msg("请选择一行数据", {
			shift : 6,
			offset : "5%"
		});
		return null;
	}
	return rows;
}

/**
 * 获取状态
 * 
 * @param value
 * @returns {String}
 */
function getStatus(value, html) {
	var newvalue = "";
	switch (value) {
	case 0:
		newvalue = "待支付";
		if (html) {
			newvalue = '<span style="color:#CC0000;">' + newvalue + '</span>';
		}
		break;
	case 1:
		newvalue = "待发货";
		if (html) {
			newvalue = '<span style="color:#CCCC00;">' + newvalue + '</span>';
		}
		break;
	case 2:
		newvalue = "已发货";
		if (html) {
			newvalue = '<span style="color:#009933;">' + newvalue + '</span>';
		}
		break;
	case 3:
		newvalue = "已配送";
		if (html) {
			newvalue = '<span style="color:#0099FF;">' + newvalue + '</span>';
		}
		break;
	case 4:
		newvalue = "已签收";
		if (html) {
			newvalue = '<span style="color:#00CC66;">' + newvalue + '</span>';
		}
		break;
	case -1:
		newvalue = "已取消";
		if (html) {
			newvalue = '<span style="color:#FF6600;">' + newvalue + '</span>';
		}
		break;
	}
	return newvalue;
}

/**
 * 获取状态
 * 
 * @param value
 * @returns {String}
 */
function getOrder_type(value) {
	var newvalue = "";
	if (value == 1) {
		newvalue = "T10商城";
	} else {
		newvalue = "折扣空间";
	}
	return newvalue;
}

/**
 * 格式化日期
 * 
 * @param value
 * @returns
 */
function getDate(value) {
	if (value === "" || value === undefined || value === null) {
		return "";
	}
	var date = value instanceof Date ? value : new Date(value);
	return date.Format("yyyy-MM-dd hh:mm:ss");
}